package com.sxit.tdf.server.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sxit.tdf.server.entity.FileBucket;
import com.sxit.tdf.server.entity.vo.DeleteBucketVo;
import com.sxit.tdf.server.entity.vo.InsertFileBucketVo;
import com.sxit.tdf.server.exception.TdfException;
import com.sxit.tdf.server.response.R;
import com.sxit.tdf.server.service.IFileBucketService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 前端控制器
 *
 * @author 橙子高
 * @since 2022-02-23
 */
@RestController
@RequestMapping("/file-bucket")
@Tag(name = "FileBucketController", description = "文件筒的接口")
public class FileBucketControllel {

  @Autowired private IFileBucketService fileBucketService;

  @PostMapping("/createBucket")
  @Operation(summary = "创建文件筒")
  @SaCheckPermission("FILEFOLAD:INSERT")
  public R createBucket(@Valid @RequestBody InsertFileBucketVo insertFileBucketVo) {

    Boolean isCreate = fileBucketService.createBucket(insertFileBucketVo);

    if (isCreate) {
      return R.ok("创建成功");
    } else {
      throw new TdfException("创建失败，请联系管理员");
    }
  }

  @PostMapping("/deleteBucket")
  @Operation(summary = "删除文件筒")
  @SaCheckPermission("FILEFOLAD:DELETE")
  public R deleteBucket(@Valid @RequestBody DeleteBucketVo deleteBucketVo) {

    fileBucketService.deleteBucket(deleteBucketVo.getId());

    return R.ok("删除成功");
  }

  @Operation(summary = "获取简易的文件筒信息")
  @GetMapping("/getBucketInfo")
  @SaCheckLogin
  public R getBucketInfo() {

    List<FileBucket> list = fileBucketService.getBucketInfo();

    return R.ok().put("data", list);
  }
}
